/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-26 12:54:22
 * @LastEditTime: 2022-08-08 16:39:48
 * @LastEditors: liutq
 * @Reference:
 */
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/smartwalle/alipay/v3"
)

var seckey = "MIIEogIBAAKCAQEAhXAHT4og0oDXOQHjd9B89oBQ4d6ag/KrfZhyVoI0iieidsPxYxayTER8uQsJKA6pWiuRnzZNYpYcbJiqc1hasGMWvMKXpY00i9pjQM90azZbGjLezdkd/pPTZbRZoSdtER/GryFYGd0vZj1Rb8kk69exPZTE7JcajK8o00YXOOpDEWHsPuYXkqLI5WLIgbbGusz9m790a5Z2kAh4McX6MljdPqZdVpfyMYPXAVRAQqAbvLouftVzdseFEOB0P0DUZ1YJXkyVIw6BvfIhtbylHmmBFbgptUMROE8COhhNbcBfRsxWCsYcLY1ScxXp3X2yAC5CwiYCfv0YyWPNH2MefwIDAQABAoIBAGK2Z5Id2v4ethjEHuRp5aftcOhG5wOpp9e3yQ3aMMfrXjKB7bah+bCWEbZKRbHOdtXv2UBwyXqfFdP4dE/0EtLd2to1Ej40YaT8H+S4E6WkhmyxKUYa0ieccAyTCKNBKVw7p52zCxvHFuOJhQ8vPBMEc2fdAHkNp2Rfx9HTziYZhMCeA+/QBRF5oOoH8L5BH+f9IDQ86s1O+KBDukqaLLQm50B1BvAEE86T3qXbZ9nr5sJVUIGIeJJcpQsPT+BuZr2+6nJ9yC1TJ8yu+Xes+VmWTtU90rF5CDNf7Yy0fnRBHiBiF/uDsr+E1Zi5JmVx0DmMBgsJlSyNAXnQPyaqylECgYEAyEtBZX+4YesVx+QaOgQEwElNxDAF1G2vokdUFf8EcBingp12aKQheAfFe6mkyLkghU4HCmcZU8wLNnPh+/4iIDXRZNbPT3xzZg4kfgCm+TZlTFlSWRudYpqKbRdFXTcCoXe6nDQxzvt3IIXb/109oHGf5qHqQLCOXfkifM2+cNUCgYEAqoyppHyFKE2iTXnsZooFNdgWg8Vbk8A34eEqm1cM51lSlg7AljSrfhEpAIXK9TDf/e+SFPG+Pu66ntq6wrquiIj48h8LKTnwzTjgRIKv4gFihUtyy1Ai293L1+hRYpTWrE1PStvUYcsBtku6smZawcu+NlLnvwc6As+9N2bOnAMCgYAxNfw9WeINfNr/H9Ypxu7754bJ2xqzlLeAYZIRTXTjrcsKVnHPR/RJL2+f8JBiGMoato8YSTh51Do1CMfPSCNOUqq94eFRoDwiKPtiUfq0ZF8DITcQWa6TmXDFWOBcBLYKwgYBdvl9517fiMQCu//2ObSVz9ZkSnzQLfQBQfO2eQKBgCArjExUmKFzk2vd1bO50SXI7uZIMe/60A+xc2H915dECAn6GmW65aZQAlXw5sm9QGMb74g32VTxnw4b4CKfKBDlFWBGbsqF/WAYnGUP6bFN5ZGTG8IyCYFDoxHE5qUbwiqaIpzSPot7yYTjYAIKwI/RqL2CgehMqqqhGOX8ZHiVAoGAM6p7q15x/jmUb3C2ZlmA6IAJuW9AJZ3tZa8lpRIEQ7U2uV/NCwLgMxB+ih+lskqAE3SapS3D3RrtPsECeoDT5rXytaz6/H5JXc+22EAcFGN1QkZeIR5TgmKJwxb6WMXo8IljSRlmmP45JMsKPx5FQJ7dVjusKB0MKsgytuFlFcs="

func main1111() {
	FinishAliPayment()
}

func FinishAliPayment() {
	// appG := app.Gin{C: c}
	client, _ := alipay.New("2021000121643312", seckey, false)
	client.LoadAppPublicCertFromFile("./appCertPublicKey_2021000121643312.crt") // 加载应用公钥证书
	client.LoadAliPayRootCertFromFile("./alipayRootCert.crt")                   // 加载支付宝根证书
	client.LoadAliPayPublicCertFromFile("./alipayCertPublicKey_RSA2.crt")       // 加载支付宝公钥证书

	var p = alipay.TradeWapPay{}
	p.NotifyURL = "https://api.jinglan.art/notifyurl"
	p.Subject = "213123123"
	p.OutTradeNo = "4646461313131446460"
	p.TotalAmount = "0.01"
	p.ProductCode = "QUICK_WAP_PAY"
	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println("pay client.TradeAppPay error:", err)
		return
	}
	fmt.Println(url)
	binary, _ := url.MarshalBinary()
	fmt.Println(string(binary))
	data := make(map[string]interface{})
	data["url"] = string(binary)
	// appG.Response(http.StatusOK, 200, data)

}

func AliPayNotify(c *gin.Context) {
	client, _ := alipay.New("2021003143601351", seckey, false)
	client.LoadAppPublicCertFromFile("conf/appCertPublicKey_2016091400512715.crt") // 加载应用公钥证书
	client.LoadAliPayRootCertFromFile("conf/alipayRootCert.crt")                   // 加载支付宝根证书
	client.LoadAliPayPublicCertFromFile("conf/alipayCertPublicKey_RSA2.crt")       // 加载支付宝公钥证书
	fmt.Println(c.Request.Body)
	var noti, _ = client.GetTradeNotification(c.Request)
	if noti != nil {
		fmt.Println("交易状态为:", noti.TradeStatus)
	}
	bs, _ := json.Marshal(noti)
	var out bytes.Buffer
	json.Indent(&out, bs, "", "\t")
	fmt.Printf("data=%v\n", out.String())
	c.String(http.StatusOK, "%s", "success")
}
